#include "sobjc-commons/memory.h"
#include "sobjc-commons/bool.h"

static void free_dummy(void *);
static void *_calloc(size_t, size_t);

objc_malloc_f   objc_malloc_callback  = malloc;
objc_malloc_f   objc_malloc_atomic_callback  = malloc;
objc_realloc_f  objc_realloc_callback = realloc;
objc_calloc_f   objc_calloc_callback  = _calloc;
objc_free_f     objc_free_callback    = free;

static BOOL is_inited = NO;

static void free_dummy(void *p) {}

void objc_enable_gc()
{
  if (!is_inited) {
    GC_init();
    is_inited = YES;
  }
  objc_set_malloc_callback(GC_malloc);
  objc_set_malloc_atomic_callback(GC_malloc_atomic);
  objc_set_realloc_callback(GC_realloc);
  objc_set_calloc_callback(_calloc);
  objc_set_free_callback(free_dummy);
}

static void *_calloc(size_t n, size_t size)
{
  void *p;

  p = objc_malloc(n * size);
  memset(p, 0, n * size);
  return p;
}

